package com.yunwaikeji.gy_tool_library.utils;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yunwaikeji.gy_tool_library.info.GYRecyclerInfo;

import java.util.List;

public class GYRecyclerDivider extends RecyclerView.ItemDecoration implements GYRecyclerInfo {
    private int orientation;
    private int dividerPx;
    private List<Integer> dividerPositionList;

    private int dividerVerticalPx;
    private int dividerVerticalColor;
    private int dividerHorizontalPx;
    private int dividerHorizontalColor;
    private int dividerColorShowUp;

    private int spanCount;

    private Paint paintVertical = new Paint();
    private Paint paintHorizontal = new Paint();
    private Rect rect = new Rect();

    public GYRecyclerDivider(int orientation, int dividerPx, int dividerColor, List<Integer> dividerPositionList) {
        this.orientation = orientation;
        this.dividerPx = dividerPx;
        this.dividerPositionList = dividerPositionList;
        paintVertical.setColor(dividerColor);
        paintHorizontal.setColor(dividerColor);
    }

    public GYRecyclerDivider(int orientation, int dividerPx, int dividerColor) {
        this(orientation, dividerPx, dividerColor, null);
    }

    public GYRecyclerDivider(int orientation, int dividerHorizontalPx, int dividerHorizontalColor, int dividerVerticalPx, int dividerVerticalColor, int dividerColorShowUp) {
        this.orientation = orientation;
        this.dividerVerticalPx = dividerVerticalPx;
        this.dividerVerticalColor = dividerVerticalColor;
        this.dividerHorizontalPx = dividerHorizontalPx;
        this.dividerHorizontalColor = dividerHorizontalColor;
        this.dividerColorShowUp = dividerColorShowUp;
        paintVertical.setColor(dividerVerticalColor);
        paintHorizontal.setColor(dividerHorizontalColor);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.State state) {
        switch (orientation) {
            case DisplayStyle.VERTICAL:
            case DisplayStyle.HORIZONTAL:
                if (recyclerView.getChildPosition(view) != 0) {
                    switch (orientation) {
                        case DisplayStyle.VERTICAL:
                            outRect.top = dividerPx;
                            break;
                        case DisplayStyle.HORIZONTAL:
                            outRect.left = dividerPx;
                            break;
                    }
                }
                break;
            case DisplayStyle.PAGER_HORIZONTAL:
            case DisplayStyle.PAGER_VERTICAL:
                switch (orientation) {
                    case DisplayStyle.PAGER_HORIZONTAL:
                        outRect.left = dividerPx;
                        outRect.right = dividerPx;
                        break;
                    case DisplayStyle.PAGER_VERTICAL:
                        outRect.top = dividerPx;
                        outRect.bottom = dividerPx;
                        break;
                }
                break;
            case DisplayStyle.GRID:
                //得到View的位置
                int childPosition = recyclerView.getChildAdapterPosition(view);
                GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
                spanCount = manager.getSpanCount();

                if (dividerHorizontalPx > 0) {
                    if (dividerHorizontalPx == 1) {
                        if (childPosition % spanCount != 0) {
                            outRect.left = 1;
                        }
                    } else {
                        final float allPx = dividerHorizontalPx * (spanCount - 1);
                        final float item = allPx / spanCount;
                        float left = (dividerHorizontalPx - item) * (childPosition % spanCount);
                        outRect.left = (int) left;
                        outRect.right = (int) (item - left);
                    }
                }

                //非第一排
                if (childPosition >= spanCount) {
                    outRect.top = dividerVerticalPx;
                }
                break;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView recyclerView, RecyclerView.State state) {
        switch (orientation) {
            case DisplayStyle.VERTICAL:
            case DisplayStyle.PAGER_VERTICAL:
                drawVertical(c, recyclerView);
                break;
            case DisplayStyle.HORIZONTAL:
            case DisplayStyle.PAGER_HORIZONTAL:
                drawHorizontal(c, recyclerView);
                break;
            case DisplayStyle.GRID:
//                drawGrid(c, recyclerView);
                switch (dividerColorShowUp) {
                    case DividerColorShowUp.VERTICAL:
                        drawHorizontal(c, recyclerView);
                        drawVertical(c, recyclerView);
                        break;
                    case DividerColorShowUp.HORIZONTAL:
                        drawVertical(c, recyclerView);
                        drawHorizontal(c, recyclerView);
                        break;
                }
                break;
        }
    }

    public void drawVertical(Canvas canvas, RecyclerView recyclerView) {
        final int left = recyclerView.getPaddingLeft();
        int right = recyclerView.getWidth() - recyclerView.getPaddingRight();

        final int childCount = recyclerView.getChildCount();
        switch (orientation) {
            case DisplayStyle.VERTICAL:
                for (int i = 1; i < childCount; i++) {
                    if (dividerPositionList == null || dividerPositionList.contains(i)) {
                        final int top = recyclerView.getChildAt(i - 1).getBottom();
                        final int bottom = recyclerView.getChildAt(i).getTop();
                        rect.set(left, top, right, bottom);
                        canvas.drawRect(rect, paintVertical);
                    }
                }
                break;
            case DisplayStyle.PAGER_VERTICAL:
                for (int i = 0; i < childCount; i++) {
                    if (dividerPositionList == null || dividerPositionList.contains(i)) {
                        final int bottom = recyclerView.getChildAt(i).getTop();
                        final int top = bottom - dividerPx;
                        rect.set(left, top, right, bottom);
                        canvas.drawRect(rect, paintVertical);
                        final int top1 = recyclerView.getChildAt(i).getBottom();
                        final int bottom1 = top1 + dividerPx;
                        rect.set(left, top1, right, bottom1);
                        canvas.drawRect(rect, paintVertical);
                    }
                }
                break;
            case DisplayStyle.GRID:
                if (childCount > spanCount) {
                    for (int i = 1; i <= (childCount - 1) / spanCount; i++) {
                        final int bottom = recyclerView.getChildAt(i * spanCount).getTop();
                        final int top = bottom - dividerVerticalPx;
                        if (i == childCount / spanCount) {
                            right = recyclerView.getChildAt(childCount - 1).getRight();
                        }
                        rect.set(left, top, right, bottom);
                        canvas.drawRect(rect, paintVertical);
                    }
                }
                break;
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView recyclerView) {
        final int top = recyclerView.getPaddingTop();
        int bottom = recyclerView.getHeight() - recyclerView.getPaddingBottom();

        final int childCount = recyclerView.getChildCount();
        switch (orientation) {
            case DisplayStyle.HORIZONTAL:
                for (int i = 1; i < childCount; i++) {
                    if (dividerPositionList == null || dividerPositionList.contains(i)) {
                        final int left = recyclerView.getChildAt(i - 1).getRight();
                        final int right = recyclerView.getChildAt(i).getLeft();
                        rect.set(left, top, right, bottom);
                        c.drawRect(rect, paintHorizontal);
                    }
                }
                break;
            case DisplayStyle.PAGER_HORIZONTAL:
                for (int i = 0; i < childCount; i++) {
                    if (dividerPositionList == null || dividerPositionList.contains(i)) {
                        final int right = recyclerView.getChildAt(i).getLeft();
                        final int left = right - dividerPx;
                        rect.set(left, top, right, bottom);
                        c.drawRect(rect, paintHorizontal);
                        final int left1 = recyclerView.getChildAt(i).getRight();
                        final int right1 = left1 + dividerPx;
                        rect.set(left1, top, right1, bottom);
                        c.drawRect(rect, paintHorizontal);
                    }
                }
                break;
            case DisplayStyle.GRID:
                for (int i = 1; i < Math.min(spanCount, childCount); i++) {
                    final int right = recyclerView.getChildAt(i).getLeft();
                    final int left = recyclerView.getChildAt(i - 1).getRight();
                    final float center = (float) (left + right) / 2;
                    final float left1 = center - (float) dividerHorizontalPx / 2;
                    final float right1 = center + (float) dividerHorizontalPx / 2;
                    if (i < childCount % spanCount || childCount % spanCount == 0) {
                        bottom = recyclerView.getChildAt(childCount - 1).getBottom();
                    } else {
                        bottom = recyclerView.getChildAt(childCount - 1).getTop() - dividerVerticalPx;
                    }
                    rect.set((int) left1, top, (int) right1, bottom);
                    c.drawRect(rect, paintHorizontal);
                }
                break;
        }
    }
}
